#Author:  Andrew Simons
#Purpose:  A simple mad editor for 2D games.  
#Generates a heaer file containing an array of number that are the metadata of a level.
#

print "What do you want to do? (Enter number only)\n";
print "0 - Load meta data\n";
print "1 - New meta data\n";
$userInput = <STDIN>;
chomp($userInput);
if($userInput =~ m/0/){
    print "Enter the filename of the metadata.\n";
    $userInput = <STDIN>;
    chomp($userInput);
    open(INPUT,$userInput) or die("File failed to open.\n");
    @metaInput = <INPUT>;
    chomp(@metaInput);
    $count = 0;
    foreach $line (@metaInput){
        $line =~ s/,//g;
        $line =~ s/};\n//;
        $line =~ s/\#endif//;
        if($count == 3){
            $mapWidth = length($line);
        }
        if($count > 2){
            @line = split(//,$line);
            foreach $character (@line){
                push(@metaData,$character)
            }
        }
        $count++;
    }
    $mapHeight = $count - 5;
    print "@metaData";
}elsif($userInput =~ m/1/){
    print "What are the x and y dimension of your tile map?\n";
    $userInput = <STDIN>;
    $userInput =~ m/(\d*)\s(\d*)/; 
    $mapWidth = $1;
    $mapHeight = $2;    
    for($z =0;$z<$mapWidth*$mapHeight;$z++){
        push(@metaData,"0");
    }
}

@prevData;

    $inLoop=1;
    while($inLoop){
        #Uncomment the lines below based on which OS you are running on in order to clear teh screen.
        system("cls");#Windows system
        #system("clear");#Linux system
        print "$mapWidth $mapHeight\n";
        print "What kind of metaData do you want to create?\n";
        print "Type \"done\" when you are finished with the map.\n";
        print "Type \"ccc\" at the end of the input if you made an error in it.\n";
        print "Type \"undo\" if you want to undo the last change.\n";
        print "0 - None\n";
        print "1 - Collideable\n";
        print "2 - Death\n";
        print "3 - Icy\n";
        
        $userInput = <STDIN>;
        chomp($userInput);
        if($userInput =~ m/ccc$/){
            next;
        }elsif($userInput =~ m/(Done|done)/){
            $inLoop=0;
            last;
        }elsif($userInput =~ m/(undo|Undo)/){
            for($x =$prevStartBlockX;$x<$prevStartBlockX+$prevBlockSizeX;$x++){
                for($y=$prevStartBlockY;$y<$prevStartBlockY+$prevBlockSizeY;$y++){
                    $metaData[$x + ($y*$mapWidth)]=$prevData[($x-$prevStartBlockX)+(($y-$prevStartBlockY)*$prevBlockSizeY)];
                }
            }
            next;
        }
        $typeMetadata = $userInput;
        $userInput="";
        
        print "What are the cordinates of the upper left corner of the block of metadata you want to create?\n";
        print "What are the x and y dimension of this block?\n";
        $userInput = <STDIN>;
        $userInput =~ m/(\d*)\s(\d*)\s(\d*)\s(\d*)/; 
        $startBlockX = $1;
        $startBlockY = $2;
        $blockSizeX = $3;
        $blockSizeY = $4;
        $userInput="";
        $prevStartBlockX = $startBlockX;
        $prevStartBlockY = $startBlockY;
        $prevBlockSizeX = $blockSizeX;
        $prevBlockSizeY = $blockSizeY;
        for($x=0;$x<$prevBlockSizeX;$x++){
            for($y=0;$y<$prevBlockSizeY;$y++){
            $prevData[$x + ($y*$prevBlockSizeY)] = $metaData[($x+$prevStartBlockX)+(($y+$prevStartBlockY)*$mapWidth)]
            }
        }
        for($x =$startBlockX;$x<$startBlockX+$blockSizeX;$x++){
            for($y=$startBlockY;$y<$startBlockY+$blockSizeY;$y++){
            $metaData[$x + ($y*$mapWidth)]=$typeMetadata;
            }
        }
    }

    open(OUTPUT,">levelMetaData.h");
    print OUTPUT "\#ifndef LEVEL_BLANK__H\n";
    print OUTPUT "\#define LEVEL_BLANK__H\n";
    print OUTPUT "static const int level_BLANK_={\n";
    
    for($z =0;$z<$mapWidth*$mapHeight;$z++){
        print OUTPUT "$metaData[$z]";
        print OUTPUT ",";
        if(($z % $mapWidth) == $mapWidth-1){
            print OUTPUT "\n";
        }
    }
    
    print OUTPUT "};\n";
    print OUTPUT "#endif";
